<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">



  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">




  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/avatar.ico?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/avatar.ico?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/avatar.ico?v=6.3.0">


  <link rel="mask-icon" href="/avatar.ico?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.3.0',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="发现自己经常打比赛挂机……正好在noip前练习一下思维题">
<meta property="og:type" content="article">
<meta property="og:title" content="atcoder套刷计划">
<meta property="og:url" content="http://zory.cf/posts/84cd.html">
<meta property="og:site_name" content="Zory的个人博客">
<meta property="og:description" content="发现自己经常打比赛挂机……正好在noip前练习一下思维题">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-09-22T23:11:47.471Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="atcoder套刷计划">
<meta name="twitter:description" content="发现自己经常打比赛挂机……正好在noip前练习一下思维题">






  <link rel="canonical" href="http://zory.cf/posts/84cd.html"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>atcoder套刷计划 | Zory的个人博客</title>
  






  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?6c38ba8648d29103b8d122f998787d71";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Zory的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">人生即使不能走最短路，仍是连通图</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页Home</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />历程Archives<span class="badge">354</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类Categories<span class="badge">4</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签Tags<span class="badge">133</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-template">
    <a href="/categories/OI之路" rel="section">
      <i class="menu-item-icon fa fa-fw fa-telegram"></i> <br />OI之路</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-problems">
    <a href="/categories/刷题" rel="section">
      <i class="menu-item-icon fa fa-fw fa-list-alt"></i> <br />刷题Problems</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-friends">
    <a href="/friends/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user-circle"></i> <br />友链Friends</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-english">
    <a href="/English/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-etsy"></i> <br />English知识体系</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-boutique">
    <a href="/tags/原创精华/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-handshake-o"></i> <br />原创精华</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-top">
    <a href="/top/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-signal"></i> <br />排行榜Hot</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />找文章强烈建议使用-搜索Search</a>
        </li>
      
    </ul>
  

  
    

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="这个搜索超级好用哦！找文章必备！" spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://zory.cf/posts/84cd.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zory">
      <meta itemprop="description" content="人生即使不能走最短路，仍是连通图">
      <meta itemprop="image" content="http://ouk2t1pgs.bkt.clouddn.com/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zory的个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">atcoder套刷计划
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建于：2018-09-19 00:00:00" itemprop="dateCreated datePublished" datetime="2018-09-19T00:00:00+08:00">2018-09-19</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改于：2018-09-23 07:11:47" itemprop="dateModified" datetime="2018-09-23T07:11:47+08:00">2018-09-23</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/刷题/" itemprop="url" rel="index"><span itemprop="name">刷题</span></a></span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/posts/84cd.html#comments" itemprop="discussionUrl">
                  <span class="post-meta-item-text">评论：</span> <span class="post-comments-count valine-comment-count" data-xid="/posts/84cd.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/posts/84cd.html" class="leancloud_visitors" data-flag-title="atcoder套刷计划">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">热度：</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>发现自己经常打比赛挂机……<br>正好在noip前练习一下思维题</p>
<a id="more"></a>
<h2 id="arc102"><a href="#arc102" class="headerlink" title="arc102"></a>arc102</h2><p>之前做了c、d、e题</p>
<h3 id="1-F-Revenge-of-BBuBBBlesort"><a href="#1-F-Revenge-of-BBuBBBlesort" class="headerlink" title="1 F - Revenge of BBuBBBlesort!"></a>1 F - Revenge of BBuBBBlesort!</h3><p>9.20 难度2<br>题意：<br>给你一个长度为n的排列A，可以交换(ai,ai+1,ai+2)当且仅当ai&gt;ai−1&gt;ai−2,问A是否能经过一系列交换最后有序</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>感觉这种性质题对我来说难度很大<br>如果一次操作以i为轴心，则称对i进行操作</p>
<p>找找性质：</p>
<ol>
<li><p>从操作位置来看：<br>因为操作的条件限制，对i操作后，无论i-2和i+2是什么，都不可能再次对i-1和i+1操作<br>既然如此，i上面的数字就动不了了，所以不可能对于一个 $i \neq a_i$ 的位置操作<br>因此，如果连续三个位置， $i \neq a_i$ ，记中间的是i，那么可以直接判断无解<br>因为此时i不能动，唯一希望就是i+1把值i送过来，但这要求 i+1=ai+1 ，但又只能从i+2送过来<br>现在i+1自己都要送过来，那么i+2只能作为轴心，无法得到i</p>
</li>
<li><p>从值来看：<br>如果我一开始向左走，那么我永远只能向左走<br>证明：最初我跨越了位置x，那么我 &lt; x上的数字，而x作为转轴不可能再变化，<br>对于值x而言，无法达成反方向操作的条件</p>
</li>
</ol>
<p>综上所述，如果已经在位置上，就不能再动了，称为定点，其他的就是动点（不能存在连续三个）<br>对于每个位置p，如果上面的值是x，那么x要变成p，而每次操作，上面的数字的奇偶性不会变化<br>所以说x和p的奇偶性要相同</p>
<p>然后我们把原序列分段，每个段满足定点动点交错，<br>那么内部的值可以跳来跳去，但不能跳出去（可能这个段只有单个元素）</p>
<p>然后我们取出每个段，在内部进行判断<br>条件1，所有值的目标不能在该段之外<br>条件2，两个目标方向相同的值，不能相互跨越<br>否则意味着其中一个值没有按照规定方向跳跃，与前面的性质矛盾</p>
<p>总结：<br>感觉这道题很奇怪<br>就是你就算找到了一些性质，也不知道是否找全了<br>典型的【不做完别的题目不会开】的题<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">	<span class="keyword">const</span> <span class="keyword">int</span> MAX_N=<span class="number">310000</span>;</div><div class="line">	<span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">	<span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line">	<span class="function"><span class="keyword">int</span> <span class="title">myabs</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;<span class="keyword">return</span> x&gt;<span class="number">0</span>?x:-x;&#125;</div><div class="line"></div><div class="line">	<span class="keyword">int</span> a[MAX_N],b[MAX_N];</div><div class="line">	<span class="function"><span class="keyword">bool</span> <span class="title">check</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r)</span></div><div class="line">	</span>&#123;</div><div class="line">		<span class="keyword">int</span> left=<span class="number">0</span>,right=<span class="number">0</span>;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=l;i&lt;=r;i++)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">if</span>(a[i]&lt;l or a[i]&gt;r) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">			<span class="keyword">if</span>(a[i]&lt;i)<span class="comment">//left</span></div><div class="line">			&#123;</div><div class="line">				<span class="keyword">if</span>(left&gt;a[i]) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">				left=a[i];</div><div class="line">			&#125;</div><div class="line">			<span class="keyword">else</span> <span class="keyword">if</span>(a[i]&gt;i)</div><div class="line">			&#123;</div><div class="line">				<span class="keyword">if</span>(right&gt;a[i]) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">				right=a[i];</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="meta">#<span class="meta-keyword">define</span> GG &#123;puts(<span class="meta-string">"No"</span>);return;&#125;</span></div><div class="line">	<span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">	</span>&#123;</div><div class="line">		<span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">		&#123;</div><div class="line">			<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]);</div><div class="line">			<span class="keyword">if</span>((a[i]-i)%<span class="number">2</span>) GG<span class="comment">//奇偶性</span></div><div class="line">			b[i]=(a[i]==i);</div><div class="line">			<span class="keyword">if</span>(i&gt;=<span class="number">3</span> and !b[i<span class="number">-2</span>] and !b[i<span class="number">-1</span>] and !b[i]) GG<span class="comment">//连续三个动点</span></div><div class="line">		&#125;</div><div class="line"></div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> l=<span class="number">1</span>;l&lt;=n;l++) <span class="keyword">if</span>(b[l]==<span class="number">0</span>)<span class="comment">//动点</span></div><div class="line">		&#123;</div><div class="line">			<span class="keyword">int</span> now=<span class="number">1</span>;<span class="comment">//交替</span></div><div class="line">			<span class="keyword">int</span> r=l;<span class="keyword">while</span>(r+<span class="number">1</span>&lt;=n and b[r+<span class="number">1</span>]==now) now^=<span class="number">1</span>,r++;</div><div class="line">			<span class="keyword">if</span>(!check(l,r)) GG</div><div class="line">			l=r;</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">puts</span>(<span class="string">"Yes"</span>);</div><div class="line">	&#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div>
<h2 id="arc101"><a href="#arc101" class="headerlink" title="arc101"></a>arc101</h2><h3 id="2-E-Ribbons-on-Tree"><a href="#2-E-Ribbons-on-Tree" class="headerlink" title="2 E - Ribbons on Tree"></a>2 E - Ribbons on Tree</h3><p>9.20 难度2<br>题意：<br>给定一棵点数为偶数的树<br>求有多少种将点两两配对的方案使得<br>每一条边至少被一对匹配点之间的路径覆盖</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>非常神仙的容斥：<br>设边集T为必定不经过的边，则答案即 $\sum (-1)^{|T|} F(T)$<br>显然大小为t的联通块，任意选择的方案数 $g(t)=(t-1)(t-3)(t-5)… \times 1$<br>具体的奇偶性等细节自行处理<br>如果T把树分成了多个联通块，则答案即每个块的g(t)的乘积</p>
<p>然而指数级枚举T显然是不现实的<br>利用图是一棵树的特性，考虑树形dp<br>设 $f(x,k)$ 表示在x的子树中，有k个是最顶上的联通块<br>这个联通块是可拓展的，所以不统计内部的方案数，直到被其父亲统计</p>
<p>dp转移时，常规地枚举两次siz，然后复杂度也同样是套路：<br>把siz看做子树的每个节点，则每个点对只会在lca处被遍历到，所以复杂度 $O(n^2)$</p>
<p>转移方程的话，因为边集大小增加，<br>如果不断开，相当于合并上面那个块， $f(x,sz1+sz2)+=f(x,sz1) \times f(y,sz2)$<br>如果断开(x,y)， $f(x,sz1)+=(-1) \times f(x,sz1) \times f(y,sz2) \times g(sz2)$<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">	<span class="keyword">const</span> <span class="keyword">int</span> MAX_N=<span class="number">5100</span>;</div><div class="line">	<span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line"></div><div class="line">	<span class="keyword">int</span> hou[MAX_N],siz[MAX_N];</div><div class="line">	<span class="keyword">struct</span> Edge&#123;<span class="keyword">int</span> y,g;&#125;e[MAX_N*<span class="number">2</span>];</div><div class="line">	<span class="keyword">int</span> ln=<span class="number">0</span>;<span class="function"><span class="keyword">void</span> <span class="title">ins</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;e[++ln]=(Edge)&#123;y,hou[x]&#125;;hou[x]=ln;&#125;</div><div class="line"></div><div class="line">	<span class="keyword">const</span> ll MOD=<span class="number">1e9</span>+<span class="number">7</span>;</div><div class="line">	<span class="function"><span class="keyword">void</span> <span class="title">addup</span><span class="params">(ll &amp;x,ll y)</span> </span>&#123;x=(x+y)%MOD;&#125;</div><div class="line"></div><div class="line">	ll g[MAX_N],f[MAX_N][MAX_N],tmp[MAX_N];</div><div class="line">	<span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> fa)</span></div><div class="line">	</span>&#123;</div><div class="line">		siz[x]=<span class="number">1</span>;f[x][<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> k=hou[x];k&gt;<span class="number">0</span>;k=e[k].g)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">int</span> y=e[k].y;<span class="keyword">if</span>(y==fa) <span class="keyword">continue</span>;</div><div class="line">			dfs(y,x);</div><div class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=siz[x]+siz[y];i++) tmp[i]=<span class="number">0</span>;</div><div class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> sz1=<span class="number">0</span>;sz1&lt;=siz[x];sz1++)</div><div class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> sz2=<span class="number">0</span>;sz2&lt;=siz[y];sz2++)</div><div class="line">				&#123;</div><div class="line">					addup(tmp[sz1+sz2],f[x][sz1]*f[y][sz2]%MOD);</div><div class="line">					<span class="keyword">if</span>(sz2%<span class="number">2</span>==<span class="number">0</span>) addup(tmp[sz1],-f[x][sz1]*f[y][sz2]%MOD*g[sz2]%MOD);</div><div class="line">				&#125;</div><div class="line">			siz[x]+=siz[y];</div><div class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=siz[x];i++) f[x][i]=tmp[i];</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">	</span>&#123;</div><div class="line">		<span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n<span class="number">-1</span>;i++)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">int</span> x,y;<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&amp;x,&amp;y);</div><div class="line">			ins(x,y);ins(y,x);</div><div class="line">		&#125;</div><div class="line">		g[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i&lt;=n;i+=<span class="number">2</span>) g[i]=g[i<span class="number">-2</span>]*(i<span class="number">-1</span>)%MOD;</div><div class="line"></div><div class="line">		dfs(<span class="number">1</span>,<span class="number">0</span>);</div><div class="line">		ll ans=<span class="number">0</span>;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> sz=<span class="number">2</span>;sz&lt;=n;sz+=<span class="number">2</span>) addup(ans,f[<span class="number">1</span>][sz]*g[sz]%MOD);</div><div class="line">		addup(ans,MOD);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"%lld"</span>,ans);</div><div class="line">	&#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

</div></div>
<h2 id="arc100"><a href="#arc100" class="headerlink" title="arc100"></a>arc100</h2><h3 id="3-Or-Plus-Max"><a href="#3-Or-Plus-Max" class="headerlink" title="3 Or Plus Max"></a>3 Or Plus Max</h3><p>9.20 难度2<br>题意：<br>又看错题了……细思极恐<br>给定一个正整数 n(n≤18)<br>然后给定一行共 $2^n$ 个正整数 a0,a1,⋯,a2n−1<br>对于每一个 k( $1≤k&lt;2^n$ )，输出满足 i OR j≤k 的最大 ai+aj 值。</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>我能想到的最好的做法：<br>如果能得出 $i|j=k$ 的情况，那么前缀max就是答案<br>然而很难算出来<br>但如果能得出 $i|j \in k$ 的答案，那么前缀max也是可以的<br>而这个显然好算很多，i和j基本没关系了</p>
<p>枚举每个k，然后枚举其子集，维护最大和次大<br>时间复杂度： $O(3^n)$<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">int</span> a[MAX_N],mx[MAX_N];</div><div class="line"><span class="keyword">int</span> ans[MAX_N];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">	<span class="keyword">int</span> m=<span class="number">1</span>&lt;&lt;n;</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;m;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]);</div><div class="line"></div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k&lt;m;k++)</div><div class="line">	&#123;</div><div class="line">		ans[k]=mymax(a[<span class="number">0</span>],ans[k<span class="number">-1</span>]);</div><div class="line">		mx[k]=a[<span class="number">0</span>];</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> u=k;u&gt;<span class="number">0</span>;u=(u<span class="number">-1</span>)&amp;k)</div><div class="line">			ans[k]=mymax(ans[k],a[u]+mx[k]),mx[k]=mymax(mx[k],a[u]);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans[k]);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>然而4亿在atcoder上面居然只需要760ms</p>
<p>正解：<br>既然我们的答案和具体子集没有关系，只关心其最大值和次大值<br>可以不用枚举子集，而是用子集来更新父亲<br>对于一个集T，不一定所有的子集都直接更新到T，也可能先经过T的子集，但这样一定不会漏<br>时间复杂度降低到了  $O(n2^n)$<br>据说这个技巧有个更深入的应用：Fast Zeta Transform<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">int</span> a[MAX_N];</div><div class="line"><span class="keyword">int</span> f[MAX_N],g[MAX_N];<span class="comment">//最大和次大值的位置</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> pos)</span></div><div class="line"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(pos&lt;<span class="number">0</span> or pos==f[x] or pos==g[x]) <span class="keyword">return</span>;</div><div class="line">	<span class="keyword">if</span>(a[pos]&gt;=a[f[x]]) g[x]=f[x],f[x]=pos;</div><div class="line">	<span class="keyword">else</span> <span class="keyword">if</span>(a[pos]&gt;=a[g[x]]) g[x]=pos;</div><div class="line">&#125;</div><div class="line"><span class="keyword">int</span> bin[<span class="number">30</span>];</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	bin[<span class="number">0</span>]=<span class="number">1</span>;<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;<span class="number">30</span>;i++) bin[i]=bin[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;</div><div class="line"></div><div class="line">	<span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;bin[n];i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;a[i]),f[i]=i,g[i]=<span class="number">-1</span>;</div><div class="line"></div><div class="line">	<span class="keyword">int</span> ans=<span class="number">0</span>;</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">0</span>;k&lt;bin[n];k++)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;n;i++)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">if</span>(k&amp;bin[i]) <span class="keyword">continue</span>;</div><div class="line">			update(k+bin[i],f[k]);</div><div class="line">			update(k+bin[i],g[k]);</div><div class="line">		&#125;</div><div class="line">		ans=mymax(ans,a[f[k]]+a[g[k]]);</div><div class="line">		<span class="keyword">if</span>(k&gt;<span class="number">0</span>) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">&#125;;</div></pre></td></tr></table></figure></p>

</div></div>
<h2 id="arc099"><a href="#arc099" class="headerlink" title="arc099"></a>arc099</h2><h3 id="4-E-Independence"><a href="#4-E-Independence" class="headerlink" title="4 E - Independence"></a>4 E - Independence</h3><p>9.21 难度1<br>题意：<br>给定一个有 n 个节点， m条边的无向图，保证没有自环和重边。<br>请你把所有的 n 个节点分成两组，同组中的任意两个节点之间都有边直接连接。<br>问连接同组节点的总边数最小为多少？如果不存在合法的划分方案，则输出 −1</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>我能想到的最好做法：<br>题目要求分成两个团，取补图后就是分成两个独立集<br>那么这个可以二分图染色，因为边意味着排斥关系<br>然后我们需要最小化 $min{ \frac{a(a-1)+b(b-1)}{2} }$<br>因为染色的时候我们会先入为主，那其实是可以整体取反的，也就是交换a和b<br>为了求最小值，我想到二维背包，但时间为 $O(n^3)$<br>感觉3亿在atcoder上应该是能跑过去的……有了上一道题的经验</p>
<p>我tm在想些什么？？？<br>a+b=n，做个屁的二维背包……<br>气到不想打</p>

</div></div>
<h2 id="arc098"><a href="#arc098" class="headerlink" title="arc098"></a>arc098</h2><h3 id="5"><a href="#5" class="headerlink" title="5"></a>5</h3><p>9.21 难度2<br>题意：<br>给你一个长度为n的整数序列，让你求出满足以下条件的(l, r)的对数：<br>其异或和=其和</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>能想到的最好做法：<br>维护一个前缀异或和a，前缀和b<br>$a[r]^a[l-1]=b[r]-b[l-1]$<br>然而异或没法和四则运算一起化柿子，复杂度只能是 $O(n^2)$</p>
<p>正解：<br>刚问出来就被秒掉了<br>$0 xor 0=0,0+0=0$<br>$0 xor 1=1,0+1=1$<br>$1 xor 1=0,1+1=2$<br>唯一的差异就是进位<br>而这个差异是没有办法消除的，只能避免<br>所以该区间一定不会在同一个位置上存在超过一个1<br>所以具有单调性</p>

</div></div>
<h3 id="6-E-Range-Minimum-Queries"><a href="#6-E-Range-Minimum-Queries" class="headerlink" title="6 E - Range Minimum Queries"></a>6 E - Range Minimum Queries</h3><p>9.21 难度2<br>题意：<br>给定一个n个数的数列和两个整数数K,Q，执行Q次操作：选择一段长度为K的区间，删除其中的最小值。<br>问：执行Q次操作后，被删除的数的最小值和最大值之差 的最小值是多少？</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>max-min的最小值显得很复杂<br>但因为取得都是原本就有的数，所以可以枚举min，然后找最小的max<br>因为min确定了，那么不能有任何区间包含小于min的数，这些数把整个区间分成很多段<br>对于每个长度为len的段，只能取出前面len-k小的数<br>把每个段能贡献的所有数排序，其中第q小的就是答案</p>

</div></div>
<h2 id="arc097"><a href="#arc097" class="headerlink" title="arc097"></a>arc097</h2><h3 id="7-D-Equals"><a href="#7-D-Equals" class="headerlink" title="7 D - Equals"></a>7 D - Equals</h3><p>9.21 难度2<br>题意：<br>给出可交换的两个位置，和一个排列，最大化pi=i的位置</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>这都没想出来……<br>对于能间接互相交换的位置，假设有a要和b交换，则总是能够a到b，<br>此时b被挤开，跳到a，然后中间的部分不会发生改变</p>
<p>所以，可以用并查集维护间接到达关系，然后询问能够回到原本位置即可</p>

</div></div>
<h3 id="8-E-Sorted-and-Sorted"><a href="#8-E-Sorted-and-Sorted" class="headerlink" title="8 E - Sorted and Sorted"></a>8 E - Sorted and Sorted</h3><p>9.21 难度1<br>题意：<br>排成一列的2N个球，有黑球和白球，黑球和白球上面都写了1-N的数字，给定一个操作：swap相邻两个球。问最少操作次数使得白球和黑球上的序号都分别递增。</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>从简单问题入手，如果给出一个n的排列，要让a[i]=i，只能相邻交换<br>此时因为每次交换只能消除一个逆序对，所以答案是逆序对数<br>此时所谓逆序对即原位置p1，终位置p2， $p1[i]&gt;p1[j]且p2[i]&lt;p2[j]$</p>
<p>那么回到本题，求出一个最优秀的终止状态p2，就能得出答案了<br>那么，白球和黑球内部要有序，但交错的顺序不确定<br>即使暴力也不好枚举，但因为黑白内部的顺序已经确定，不难想到可以用类似字符串匹配的方式dp</p>
<p>设 $f(x \leq 2n,a,b)$ 表示填写到第x位，白色填了a个，黑色填了b个的逆序对最少个数<br>显然i=a+b，实现的时候需要去除一维<br>转移的话，关键就是要快速地计算新逆序对<br>设白色的id为x，则对应黑色的id为n+x<br>当放白色， $p1[t]&gt;p1[a]，t=1 \to a-1和n+1 \to n+b$<br>当放黑色， $p1[t]&gt;p1[n+b]，t=1 \to a和n+1 \to n+b-1$<br>这个东西可以预处理一下，然后就能达到 $O(n^2)$</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Zory-2018</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdio&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstdlib&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;map&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;set&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;deque&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bitset&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">namespace</span> mine</div><div class="line">&#123;</div><div class="line">	<span class="keyword">const</span> <span class="keyword">int</span> MAX_N=<span class="number">2100</span>;</div><div class="line">	<span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">0x3f3f3f3f</span>;</div><div class="line">	<span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</div><div class="line">	<span class="function"><span class="keyword">int</span> <span class="title">mymax</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&gt;y?x:y;&#125;</div><div class="line">	<span class="function"><span class="keyword">int</span> <span class="title">mymin</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;<span class="keyword">return</span> x&lt;y?x:y;&#125;</div><div class="line"></div><div class="line">	<span class="keyword">int</span> p1[MAX_N*<span class="number">2</span>];</div><div class="line">	<span class="keyword">int</span> f[MAX_N*<span class="number">2</span>][MAX_N];</div><div class="line">	<span class="keyword">int</span> nw[MAX_N*<span class="number">2</span>][MAX_N*<span class="number">2</span>];</div><div class="line">	<span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></div><div class="line">	</span>&#123;</div><div class="line">		<span class="keyword">int</span> n;<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;n);</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">2</span>*n;i++)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">char</span> s[<span class="number">5</span>];<span class="keyword">int</span> id;<span class="built_in">scanf</span>(<span class="string">"%s%d"</span>,s,&amp;id);</div><div class="line">			p1[id+n*(s[<span class="number">0</span>]==<span class="string">'B'</span>)]=i;</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">2</span>*n;i++)</div><div class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> t=<span class="number">1</span>;t&lt;=<span class="number">2</span>*n;t++)</div><div class="line">				nw[i][t]=nw[i][t<span class="number">-1</span>]+(p1[t]&gt;p1[i]);</div><div class="line"></div><div class="line">		<span class="built_in">memset</span>(f,<span class="number">63</span>,<span class="keyword">sizeof</span> f);f[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">0</span>;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> x=<span class="number">1</span>;x&lt;=<span class="number">2</span>*n;x++)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> a=<span class="number">0</span>;a&lt;=x and a&lt;=n;a++)</div><div class="line">			&#123;</div><div class="line">				<span class="keyword">int</span> b=x-a;</div><div class="line">				<span class="keyword">if</span>(a&gt;<span class="number">0</span>) f[x][a]=mymin(f[x][a],</div><div class="line">					f[x<span class="number">-1</span>][a<span class="number">-1</span>]+nw[a][a<span class="number">-1</span>]+nw[a][n+b]-nw[a][n]</div><div class="line">				);</div><div class="line">				<span class="keyword">if</span>(b&gt;<span class="number">0</span>) f[x][a]=mymin(f[x][a],</div><div class="line">					f[x<span class="number">-1</span>][a]+nw[n+b][a]+nw[n+b][n+b<span class="number">-1</span>]-nw[n+b][n]</div><div class="line">				);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"%d"</span>,f[<span class="number">2</span>*n][n]);</div><div class="line">	&#125;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">	mine::main();</div><div class="line">&#125;</div></pre></td></tr></table></figure>

</div></div>
<h3 id="9-E-Everything-on-It"><a href="#9-E-Everything-on-It" class="headerlink" title="9 E - Everything on It"></a>9 E - Everything on It</h3><p>9.21 难度2<br>题意：<br>拉面有 n 种配料 每种配料可以选择加入到拉面中，也可以不加入<br>一共 $2^n$ 种组合 有人来订购一些拉面<br>要求：<br>每种拉面配料不能相同。<br>每种配料在全部的面中至少出现过两次。</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<p>这道题一眼容斥来搞【不能出现少于两次】的这个条件<br>$ANS=\sum_{k=0}^n (-1)^k C(n,k) f(k)$<br>其中f(k)表示有k个颜色只能不用或用一次，剩下n-k个颜色任意放，但不能出现两个完全相同的组合数<br>然后到这里我就不知道怎么处理不能完全相同这个问题了</p>
<p>f(k)应该分两部分去思考<br>① 不合法的k个颜色<br>那么相当于这k个元素，要么不放，要么放进一个集合中<br>假设有t个非空集合，那么这个是类似于第二类斯特林数的（刚学……）<br>递推式： $g(k,t)=g(k-1,t-1)+g(k-1,t) \times (t+1)，0 \leq t \leq k$<br>解释：在原本第二类斯特林数的基础上，加上【可以丢掉】这个选项<br>可以是把第k个元素单独放进第t个位置，也可能第t个集合是混合的，再或者丢掉<br>②其他的n-k个颜色<br>这里非常巧妙，也是我一直不知道怎么解决的地方<br>方案数量为 $2^{n-k}$<br>那么对于那k个非法元素，本来觉得超级复杂，<br>其实因为每个最多出现一次，不同的碗一定不会重复<br>所以是 $2^{(n-k)t}$</p>
<p>而合法元素的话，不应该和碗的数量扯上关系，<br>而是考虑把每个方案看作一个碗，考虑这个碗是否出现<br>所以是 $2^{2^{n-k}}$</p>
<p>综上所述， $f(k)=\sum_{t=0}^k g(k,t) \times 2^{(n-k)t} \times 2^{2^{n-k}}$<br>套一个小费马定理即可</p>

</div></div>
<h3 id="11-F-Sweet-Alchemy"><a href="#11-F-Sweet-Alchemy" class="headerlink" title="11 F - Sweet Alchemy"></a>11 F - Sweet Alchemy</h3><p>9.23 难度2<br>题意：<br>n≤50的树，每个点有权值，现要选点（可多次选一个点）使点数尽量多，如下限制：<br>选的总权值不超过C≤1e9；<br>ci表示i选的次数，pi表示i的父亲，那么cpi≤ci≤cpi+D，D≤1e9是给定常数。</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div></pre></td></tr></table></figure>

</div></div>
<h2 id="arc096"><a href="#arc096" class="headerlink" title="arc096"></a>arc096</h2><h3 id="10"><a href="#10" class="headerlink" title="10"></a>10</h3><p>9.23 难度2<br>题意：</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div></pre></td></tr></table></figure>

</div></div>
<h3 id="11"><a href="#11" class="headerlink" title="11"></a>11</h3><p>9.23 难度2<br>题意：</p>
<div><div class="fold_hider"><div class="close hider_title">请先思考后再展开</div></div><div class="fold">
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"></div></pre></td></tr></table></figure>

</div></div>

      
    </div>

    
    
    

    

    
      


    

	
		<div>
				<p id="div-border-left-purple">
			   <b>本文基于<a target="_blank" title="Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)" href="http://creativecommons.org/licenses/by-sa/4.0/deed.zh"> 知识共享署名-相同方式共享 4.0 </a>国际许可协议发布</b><br/>
				<span>
				<b>本文地址：</b><a href="/posts/84cd.html" title="atcoder套刷计划">http://zory.cf/posts/84cd.html</a><br/><b>转载请注明出处，谢谢！</b>
				</span>
				</p>
		</div>
    

    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>哪怕是一杯奶茶，也将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="Zory 微信支付"/>
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/59da.html" rel="next" title="【训练】算法竞赛进阶指南-4数据结构 8题">
                <i class="fa fa-chevron-left"></i> 【训练】算法竞赛进阶指南-4数据结构 8题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/7761.html" rel="prev" title="【NOIAC37】染色">
                【NOIAC37】染色 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ouk2t1pgs.bkt.clouddn.com/avatar.jpg"
                alt="Zory" />
            
              <p class="site-author-name" itemprop="name">Zory</p>
              <p class="site-description motion-element" itemprop="description">人生即使不能走最短路，仍是连通图</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">354</span>
                    <span class="site-state-item-name">文章</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">133</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/Zory123" target="_blank" title="GitHub" rel="external nofollow"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:1134430323@qq.com" target="_blank" title="E-Mail" rel="external nofollow"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

<script type="text/javascript" src="https://api.lwl12.com/hitokoto/main/get?encode=js&charset=utf-8"></script>
<div id="lwlhitokoto">
    <script>
        lwlhitokoto()
    </script>
</div>

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#arc102"><span class="nav-text">arc102</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-F-Revenge-of-BBuBBBlesort"><span class="nav-text">1 F - Revenge of BBuBBBlesort!</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#arc101"><span class="nav-text">arc101</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-E-Ribbons-on-Tree"><span class="nav-text">2 E - Ribbons on Tree</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#arc100"><span class="nav-text">arc100</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-Or-Plus-Max"><span class="nav-text">3 Or Plus Max</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#arc099"><span class="nav-text">arc099</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-E-Independence"><span class="nav-text">4 E - Independence</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#arc098"><span class="nav-text">arc098</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5"><span class="nav-text">5</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-E-Range-Minimum-Queries"><span class="nav-text">6 E - Range Minimum Queries</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#arc097"><span class="nav-text">arc097</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-D-Equals"><span class="nav-text">7 D - Equals</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-E-Sorted-and-Sorted"><span class="nav-text">8 E - Sorted and Sorted</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-E-Everything-on-It"><span class="nav-text">9 E - Everything on It</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-F-Sweet-Alchemy"><span class="nav-text">11 F - Sweet Alchemy</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#arc096"><span class="nav-text">arc096</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#10"><span class="nav-text">10</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11"><span class="nav-text">11</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zory</span>

  

  
</div>


  









  <div class="footer-custom">Hosted by <a target="_blank" rel="external nofollow" href="https://pages.coding.me"><b>Coding Pages</b></a></div>


        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>












  















  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



	





  





  








  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(function (item) {
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: true,
        appId: 'zMDFLwV3WMlWrOYhJ7tOad3M-gzGzoHsz',
        appKey: 'cJDLxDoNgzq0JJUWQFmpCRdw',
        placeholder: '对这篇文章有什么看法？大胆地说出来吧',
        avatar:'wavatar',
        guest_info:guest,
        pageSize:'20' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('2');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("zMDFLwV3WMlWrOYhJ7tOad3M-gzGzoHsz", "cJDLxDoNgzq0JJUWQFmpCRdw");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            
            counter.save(null, {
              success: function(counter) {
                
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(counter.get('time'));
                
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            
              var newcounter = new Counter();
              /* Set ACL */
              var acl = new AV.ACL();
              acl.setPublicReadAccess(true);
              acl.setPublicWriteAccess(true);
              newcounter.setACL(acl);
              /* End Set ACL */
              newcounter.set("title", title);
              newcounter.set("url", url);
              newcounter.set("time", 1);
              newcounter.save(null, {
                success: function(newcounter) {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
                },
                error: function(newcounter, error) {
                  console.log('Failed to create');
                }
              });
            
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  

  
    
      <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      },
      TeX: {equationNumbers: { autoNumber: "AMS" }}
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/mathjax@2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

    
  


  
  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook,GooglePlus,Evernote,Vkontakte";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  
  
  
  <script src="/lib/pangu/dist/pangu.min.js?v=3.3"></script>
  <script type="text/javascript">pangu.spacingPage();</script>


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=6.3.0"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=6.3.0"></script>


  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('Copy').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('Copied')
          else $(this).text('Copy failed')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('Copy')
        }, 300)
      }).append(e)
    })
  </script>


</body>
</html>
